/**********************************************************************/
/*
Author: Karan Makkar
Created: Jan  2024
Description: Report Applied/Won in National Surveys by Online Survey Response
Output: TableA28
*/
/**********************************************************************/

/*----------------------------------------------------*/
* Section: Setup
/*----------------------------------------------------*/

* include filepaths 
if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

clear all
set more off
set matsize 11000

* Log
cap log close
global prefix: display %tdCYND td(`c(current_date)')
log using "$KP_logs/${prefix}_online_national_applyselect_compare.txt", text replace

/*----------------------------------------------------*/
* Section: Run programs and set dataset filepaths
/*----------------------------------------------------*/

* Set filepaths for dataset
global aug20 "$KP_deid_sakernas/Clean/sak_aug20_deid_clean_merged.dta"
global feb21 "$KP_deid_sakernas/Clean/sak_feb21_deid_clean_merged.dta"
global aug21 "$KP_deid_sakernas/Clean/sak_aug21_deid_clean_merged.dta"
global sep20 "$KP_deid_susenas/Clean/sus_sep20_deid_clean_merged.dta"
global mar21 "$KP_deid_susenas/Clean/sus_mar21_deid_clean_merged.dta"
global sep21 "$KP_deid_susenas/Clean/sus_sep21_deid_clean_merged.dta"
global mar22 "$KP_deid_susenas/Clean/sus_mar22_deid_clean_merged.dta"

/*----------------------------------------------------------------*/
* Save Online Survey Tempfiles
/*----------------------------------------------------------------*/

**** 2021 Survey ***  
use "$KP_deid_survey/2021/Clean/071323_survey_21_deid_clean_merged.dta", clear

* Keep only those sent survey.
keep if sent_survey==1 | !mi(progress)

* Tag respondent
gen respondent21 = completed ==1 & consented == 1 & verified ==1

tempfile s21
sa `s21', replace

**** 2022 Survey ***  
use "$KP_deid_survey/2022/Clean/071323_survey_22_deid_clean_merged.dta", clear

*One obs per person
bysort anon_id4 (batch): keep if _n==1

*Keep only those sent survey.
keep if sent_survey==1 | !mi(progress)

*Tag respondents
gen respondent22 = completed == 1 & consented == 1 & verified == 1

tempfile s22
sa `s22', replace

/*----------------------------------------------------------------*/
* SAKERNAS Regressions
/*----------------------------------------------------------------*/

* Load data and append
use "${aug20}", clear
append using "${feb21}"
append using "${aug21}"
append using "${sep20}"
append using "${mar21}"
append using "${sep21}"
append using "${mar22}"

* Win before survey var
gen win_before_survey = .
replace win_before_survey  = ever_win_3 if sak_round == 5 & inrange(batch, 2, 3)
replace win_before_survey  = ever_win_11 if sak_round == 6 & inrange(batch, 2, 11)
replace win_before_survey  = ever_win_17 if sak_round == 7 & inrange(batch, 2, 17)
replace win_before_survey  = ever_win_5 if sus_round == 5 & inrange(batch, 2, 5)
replace win_before_survey  = ever_win_11 if sus_round == 6 & inrange(batch, 2, 11)
replace win_before_survey  = ever_win_18 if sus_round == 7 & inrange(batch, 2, 18)
replace win_before_survey  = ever_win_22 if sus_round == 8 & inrange(batch, 2, 22)
gegen win_before_survey = max(win_before_survey), by(anon_id4 sus_round sak_round) replace

*Apply before survey var
gen apply_before_survey = 0
replace apply_before_survey  = 1 if sak_round == 5 & first_apply_batch <=3
replace apply_before_survey  = 1 if sak_round == 6 & first_apply_batch <=11
replace apply_before_survey  = 1 if sak_round == 7 & first_apply_batch <=17
gegen apply_before_survey = max(apply_before_survey), by(anon_id4 sus_round sak_round) replace

* combine get_pk and hh_pk_win 
gen pk_win_comb = hh_pk_win if sus_round == 6 | sus_round ==7
replace pk_win_comb = get_pk if sus_round == 5 | sus_round ==8

replace report_selected = pk_win_comb if !mi(sus_round)

* Merge in 2021 data
fmerge m:1 anon_id4 using `s21', keep(1 3) keepusing(respondent21) gen(_merge21)
* Merge in 2022 data
fmerge m:1 anon_id4 using `s22', keep(1 3) keepusing(respondent22) gen(_merge22)

*Tag one observation per person-round cell
bysort anon_id4 sus_round sak_round: keep if _n ==1

* Regress Report selected/applied on Respond to Online Survey
* Report selected, 2021 Respondent
eststo r1: reg report_selected respondent21 if _merge21 ==3 & win_before_survey ==1, vce(cluster anon_id4)
summ report_selected if respondent21 ==0 & _merge21 ==3 & win_before_survey ==1
estadd scalar cmean = r(mean)

* Report selected, 2022 Respondent
eststo r2: reg report_selected respondent22 if _merge22 ==3 & win_before_survey ==1, vce(cluster anon_id4)
summ report_selected if respondent22 ==0 & _merge22 ==3 & win_before_survey ==1
estadd scalar cmean = r(mean)

* Report applied, 2021 Respondent
eststo r3: reg report_applied respondent21 if _merge21 ==3 & apply_before_survey ==1, vce(cluster anon_id4)
summ report_applied if respondent21 ==0 & _merge21 ==3 & apply_before_survey ==1
estadd scalar cmean = r(mean)

* Report applied, 2022 Respondent
eststo r4: reg report_applied respondent22 if _merge22 ==3 & apply_before_survey ==1, vce(cluster anon_id4)
summ report_applied if respondent22 ==0 & _merge22 ==3 & apply_before_survey ==1
estadd scalar cmean = r(mean)

/*----------------------------------------------------------------*/
* Make Tables
/*----------------------------------------------------------------*/

*Labelling
la var respondent21 "Responded to Online Survey"
la var respondent22 "Responded to Online Survey"

* Panel A: 2021 Online Response
  esttab r3 r1 using "$KP_output/tables/descriptive/tex/TableA28.tex", ///
  b(%9.3f) se(%9.3f) drop(_cons) ///
  stats(N cmean, label("Observations" "Report Not-Applied Mean") fmt(%5.0fc %9.3fc)) ///
  replace label ///
  star(* 0.10 ** 0.05 *** 0.01) ///
  mtitles("Report Applied" "Report Won") ///
  nonum ///
  prehead("\hline \hline") prefoot("\\ ") ///
  posthead(`"& (1) & (2)  \\"' `"\hline \\"' `"\textit{Panel A: 2021 Online Survey} \\"') postfoot(`"Batches & 7-14, 16-17 & 7-14, 16-22 \\"' `"\hline \\"')

* Panel B: 2022 Online Response
  esttab r4 r2 using "$KP_output/tables/descriptive/tex/TableA28.tex", ///
  b(%9.3f) se(%9.3f) drop(_cons) ///
  stats(N cmean, label("Observations" "Report Not-Won Mean") fmt(%5.0fc %9.3fc)) ///
  append label ///
  star(* 0.10 ** 0.05 *** 0.01) ///
  nomtitles ///
  nonum ///
  prehead("") posthead(`"\textit{Panel B: 2022 Online Survey} \\"') ///
  prefoot("\\ ") postfoot(`"Batches & 7-14, 16-17 & 7-14, 16-22 \\"' `"\hline"') 